
%cd ('C:\Programme\Matlab71\work\RES') 
%cd ('W:\Projects\Hoffmann_Shcherbakova\Programs_RES')
load Main_Variables.mat
load SmallFirms_Importance.mat
load Shapi_50_57.mat
load SDD.mat
load gdp_bea.mat
load NBER.mat
load IndSpec.mat


%cd ('C:\Programme\Matlab71\work')

%T=T-3; %1964-2005
%T=21 %1964-84
Intra=Intra(2:end,:);%from 1964
States={'Alab';%1
    'Alas';%2
    'Ariz';%3
    'Arka';%4
    'Cali';%5
    'Colo';%6
    'Conn';%7
    'Dela';%8
    'DCol';%9
    'Flor';%10
    'Geor';%11
    'Hawa';%12
    'Idah';%13
    'Illi';%14
    'Indi';%15
    'Iowa';%16
    'Kans';%17
    'Kent';%18
    'Loui';%19
    'Main';%20
    'Mary';%21
    'Mass';%22
    'Mich';%23
    'Minn';%24
    'Miss';%25
    'Miso';%26
    'Mont';%27
    'Nebr';%28
    'Neva';%29
    'NHam';%30
    'NJer';%31
    'NMex';%32
    'NYor';%33
    'NCar';%34
    'NDak';%35
    'Ohio';%36
    'Okla';%37
    'Oreg';%38
    'Penn';%39
    'RhoI';%40
    'SCar';%41
    'SDak';%42
    'Tenn';%43
    'Texa';%44
    'Utah';%45
    'Verm';%46
    'Virg';%47
    'Wash';%48
    'WVir';%49
    'Wisc';%50
    'Wyom'};%51



switch state_group
 case {'all'}
    
 StateSelect=(1:K);

 case {'Low_Shapi_64_75'};    
    StateSelect=[40,22,12,21,31,47,23,33,7,49,2,9,36,29,30,8,39];%
    
 case {'Middle_Shapi_64_75'};  
     StateSelect=[41,14,10,15,3,45,5,11,1,19,48,20,32,50,26,34,43];
     
 case {'High_Shapi_64_75'};
     StateSelect=[6,38,46,18,44,24,37,25,51,17,4,13,27,28,16,42,35];
     
 case {'Low_Shapi_50_55'};    
    StateSelect=[9,22,40,2,33,7,12,31,49,23,21,39,36,8,30,14,47];
    
 case {'Middle_Shapi_50_55'};  
     StateSelect=[20,15,5,45,48,19,29,46,6,10,26,41,50,32,11,1,44];
     
 case {'High_Shapi_50_55'};
     StateSelect=[18,37,43,51,38,24,34,17,3,13,4,27,25,28,16,35,42];
     
 case {'LowHalf_Shapi_64_75'};   
      StateSelect=[40,22,12,21,31,47,23,33,7,49,2,9,36,29,30,8,39,41,14,10,15,3,45,5,11];
  
 case {'HighHalf_Shapi_64_75'}; 
     StateSelect=[1,19,48,20,32,50,26,34,43,6,38,46,18,44,24,37,25,51,17,4,13,27,28,16,42,35];    
  
 case {'LowHalf_Shapi_64_75_dereg'};
     StateSelect=[40,22,21,31,47,33,7,2,9,36,29,8,39,41,3,45,5,11];
      
  case{'LowHalf_Shapi_64_75_notdereg'}; 
      StateSelect=[12,23,49,30,14,10,15];
           
 case {'HighHalf_Shapi_64_75_dereg'};
     StateSelect=[1,20,34,46,13,42];
     
 case {'HighHalf_Shapi_64_75_notdereg'};  
     StateSelect=[19,48,32,50,26,43,6,38,18,44,24,37,25,51,17,4,27,28,16,35];
          
 case {'LowHalf_SBE77_dereg'};
     StateSelect=[41,7,39,36,34,8,22,29,33,9,31,40,1,11,47];% 
        
 case{'LowHalf_SBE77_notdereg'};
     StateSelect=[14,23,15,43,50,26,49,24,18,25];
        
 case{'HighHalf_SBE77_dereg'};
     StateSelect=[21,20,5,45,46,3,2,13,42];
        
 case{'HighHalf_SBE77_notdereg'};
     StateSelect=[30,44,4,19,16,6,48,37,17,28,38,12,10,32,51,27,35];
       
    case {'Low_SBE77'};
        StateSelect=[41,7,39,14,36,23,15,34,8,22,29,33,9,43,31,50,26];%
          
    case{'Middle_SBE77'} ;   
        StateSelect=[40,1,49,11,24,47,18,25,21,30,44,20,5,4,19,45,46];

    case{'High_SBE77'};
        StateSelect=[16,6,3,48,37,17,28,2,38,12,10,13,32,51,27,42,35];
        

   
otherwise
disp('State group unspecified - Program aborted!');
break

end;


K=length(StateSelect);
T=T-s+1;% till 2005


if ~exist('Region_Demean')
      Region_Demean='yes';
      
end
   
switch Region_Demean
    case 'yes'
      rse=1;
    case 'no'
        rse=0;
end
if ~exist('Time_Demean')
      Time_Demean='yes';
      
end
   
switch Time_Demean
    case 'yes'
      tfe=1;
    case 'no'
        tfe=0;
end

 if ~exist('Fixed_Effects')
      Fixed_Effects='no';
   end
 
 switch Fixed_Effects
     case 'countryfix'
         FE=1;
     case 'timefix'
         FE=2;
     case 'both'
         FE=3;
     case 'no'
        FE=0;
 end
 if ~exist('Auxilary_Term')
     Auxilary_Term=0;
 end
 

 %tfe is time-fixed effect, 
 %rse is region-fixed effect
 
 dgsp=dgsp(s:s+T-1,StateSelect)-tfe*mean(dgsp(s:s+T-1,StateSelect),2)*ones(1,K);
 dspi=dspi(s:s+T-1,StateSelect)-tfe*mean(dspi(s:s+T-1,StateSelect),2)*ones(1,K);
 ddsi=ddsi(s:s+T-1,StateSelect)-tfe*mean(ddsi(s:s+T-1,StateSelect),2)*ones(1,K);
 dc=dc(s:s+T-1,StateSelect)-tfe*mean(dc(s:s+T-1,StateSelect),2)*ones(1,K); 
 
 dgsp=dgsp-rse*ones(T,1)*mean(dgsp);
 dspi=dspi-rse*ones(T,1)*mean(dspi);
 ddsi=ddsi-rse*ones(T,1)*mean(ddsi);
 dc=dc-rse*ones(T,1)*mean(dc);
 

t=(1:T)';
t=t-sum(t)/size(t,1);
trend=t*ones(1,K);

 
%adjust state-specific variables for State Group
mu=shapi(2:T+1,:);
mu=mu(:,StateSelect);
mean_mu1=mean(mu);
mean_mu=mean_mu1-mean(mean_mu1);% mean of shapi over the whole period
mean_mu75=mean(mu(1:12,:))-mean(mean(mu(1:12,:))); %mean of shapi from 64 to 75
mu55=shapi_50_57(:,1:6)';%cut years 56 and 57
mean_mu55=mean(mu55)-mean(mean(mu55));
IndSpec=IndSpec';
IndSpec=IndSpec(:,StateSelect);
Intra=Intra(1:T,StateSelect);

 Y_channels=[dgsp(:)-dspi(:),dspi(:)-ddsi(:),ddsi(:)-dc(:),dc(:)]; 
